package it.webrules.deploymanager.core.st.scm;

import java.util.ArrayList;
import java.util.Date;
import java.util.Formatter;
import java.util.List;

public class SwitchableDirs {

	public static final int MAX_NAME_LENGHT = 45;
	public static final int CUT_ON_MAX_NAME_LENGHT = 3;

	private List<SwitchableDir> switchableDirs;

	public SwitchableDirs() {
		switchableDirs = new ArrayList<SwitchableDir>();
	}

	public List<SwitchableDir> getSwitchableDirs() {
		return switchableDirs;
	}

	public void addSwitchableDir(String name, String url, String commitmessage, Date date, Long revision, Type type) {
		switchableDirs.add(new SwitchableDir(name, url, commitmessage, date, revision, type));
	}

	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Source can checkout from or switch to:\n");

		if (switchableDirs.size() == 0) {
			builder.append("No trunk/tags/branches available.\n");
		} else {

			Formatter formatter = new Formatter();

			builder.append("Type    Name                      Date\n");
			builder.append("---------------------------------------------------------------\n");

			for (SwitchableDir switchableDir : switchableDirs) {

				String name = switchableDir.name;
				if (name.length() > MAX_NAME_LENGHT)
					name = name.substring(0, MAX_NAME_LENGHT - CUT_ON_MAX_NAME_LENGHT) + "...";

				formatter.format("%-7s %-25s %tc\n", switchableDir.type.name(), name, switchableDir.date);

			}

			builder.append(formatter.toString() + "\n");
		}

		return builder.toString();

	}
}
